package com.ericsson.enap.modules.autoCheck.job;

import com.ericsson.common.quartz.job.MainJob;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.URI;

@Component
public class HttpServerTest implements MainJob {

    @Override
    public void doTask() {
        try {
            HttpServer httpServer = HttpServer.create(new InetSocketAddress(9999), 0);
            httpServer.createContext("/syncAutoCheck", new TestHandler());
            httpServer.start();
            System.out.println("端口已成功启动");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    class TestHandler implements HttpHandler {

        @Override
        public void handle(HttpExchange httpExchange) throws IOException {
            //返回
            String rtn="success";
            httpExchange.sendResponseHeaders(200, 0);
            //获取请求路径
            URI requestURI = httpExchange.getRequestURI();
            String param = requestURI.getQuery();
            System.out.println("------------------调用成功--------------------"+param);
            OutputStream responseBody = httpExchange.getResponseBody();
            responseBody.write(rtn.getBytes());
            responseBody.close();
        }

    }
}
